<template>
  <el-dialog
    :title="row ? '编辑列' : '新增列'"
    :visible.sync="dialogVisible"
    :modal="true"
    :appendToBody="true"
    width="700px"
    v-dialog-drag
    :close-on-click-modal="false"
    top="5vh"
    @close="handleClose()"
    @open="handleOpen()"
  >
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="类型" prop="cellType">
            <el-radio-group v-model="form.cellType"  :disabled="!!row">
              <el-radio
                v-for="item in typeOptions"
                :key="item.value"
                :label="item.value"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="显示排序" prop="sortIndex">
            <el-input-number
              v-model="form.sortIndex"
              :step="1"
              step-strictly
              :min="1"
              controls-position="right"
            ></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" :loading="saveloading" @click="handleSubmit"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { checkPermi } from "@/utils/permission";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    row: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {
      form: {
        prop: null,
        sortIndex: 1, //默认排序1
        cellType: "standardText",
      },
      rules: {
        sortIndex: [
          { required: true, message: "请输入显示排序", trigger: "change" },
          { pattern: /^\d{1,3}$/, message: "最大排序999", trigger: "change" },
        ],
        cellType: [
          { required: true, message: "请选择类型", trigger: "change" },
        ]
      },
      typeOptions: [
        { value: "standardText", label: "标准内容(显示为文本)" },
        { value: "formField", label: "表单(填写区)" },
      ],
      saveloading: false,
    };
  },
  computed: {
    dialogVisible: {
      set(val) {
        this.$emit("update:visible", val);
      },
      get() {
        return this.visible;
      },
    },
  },
  methods: {
    handleClose() {
      this.$refs.ruleForm.resetFields();
    },
    handleOpen() {
      if (this.row) {
        const row = this.row;
        this.form = {
          prop: row.prop,
          // label: row.label,
          sortIndex: row.sortIndex,
          cellType: row.cellType
        };
      } else {
        // 新增时，生成prop
        this.form.prop = `prop_${new Date().getTime()}`;
        this.form.sortIndex = 1;
        this.form.cellType = "standardText";
      }
    },
    handleSubmit() {
      this.$refs.ruleForm.validate((flag) => {
        if (flag) {
          const param = { ...this.form };
          this.$emit("refresh", this.row, param);
          this.msgSuccess("保存成功！");
          this.dialogVisible = false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
